<template>
  <div>
    <div class="list-search">
      <ul class="search-list">
        <li>
          <el-input
            v-model="searchList.name"
            placeholder="请输入户主姓名"
          ></el-input>
        </li>
        <li>
          <el-input
            v-model="searchList.idCard"
            placeholder="请输入身份证号"
          ></el-input>
        </li>
        <li>
          <el-button
            type="primary"
            icon="el-icon-search"
            plain
            @click="search()"
            >查询</el-button
          >
        </li>
      </ul>
    </div>
    <el-table :data="personList" border v-loading="loading" style="width: 100%">
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column label="性别">
        <template slot-scope="scope">
          {{ sexMap[scope.row.sex] }}
        </template>
      </el-table-column>
      <el-table-column prop="birthday" label="出生日期"> </el-table-column>
      <el-table-column prop="idCard" label="身份证号"> </el-table-column>
      <el-table-column prop="mobile" label="电话号码"> </el-table-column>
      <el-table-column prop="nativePlace" label="籍贯"> </el-table-column>
      <el-table-column prop="address" label="现住址"> </el-table-column>
      <el-table-column label="户籍状态">
        <template slot-scope="scope">
          {{ personTypeMap[scope.row.personType] }}
        </template>
      </el-table-column>
      <el-table-column label="其他" type="expand" width="50px">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="户主姓名">
              <span>{{ props.row.householder }}</span>
            </el-form-item>
            <el-form-item label="备注信息">
              <span>{{
                props.row.remark ? props.row.remark : "暂无备注"
              }}</span>
            </el-form-item>
            <el-form-item v-if="props.row.personType == '2'" label="迁入日期">
              <span>{{ props.row.moveIn }}</span>
            </el-form-item>
            <el-form-item v-if="props.row.personType == '0'" label="创建日期">
              <span>{{ props.row.created }}</span>
            </el-form-item>
            <el-form-item v-if="props.row.personType == '1'" label="迁出日期">
              <span>{{ props.row.moveOut }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220px">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.personType != '1'"
            type="warning"
            size="mini"
            @click="showDialog(scope.row)"
            >修改</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="detail(scope.row.userId)"
            >详情</el-button
          >
          <el-button
            v-if="scope.row.personType != '1'"
            type="danger"
            size="mini"
            @click="moveOut(scope.row.userId)"
            >迁出</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="pageNum"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
      <el-form
        :model="modifyTemp"
        :label-width="formLabelWidth"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="modifyTemp.name"></el-input>
        </el-form-item>
        <el-form-item label="用户电话" prop="mobile">
          <el-input v-model="modifyTemp.mobile"></el-input>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <!-- <el-input v-model="modifyTemp.birthday"></el-input> -->
          <!-- <el-date-picker
            v-model="modifyTemp.birthday"
            type="datetime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期时间">
          </el-date-picker> -->
          <el-date-picker
            v-model="modifyTemp.birthday"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择出生日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="modifyTemp.sex">
            <el-option label="男" value="0"> </el-option>
            <el-option label="女" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="modifyTemp.nation"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="modifyTemp.idCard"></el-input>
        </el-form-item>
        <el-form-item label="现住址" prop="address">
          <el-input v-model="modifyTemp.address"></el-input>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="modifyTemp.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="modify()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { storage } from "../../utils/storage";
import { utils } from "../../utils/common.js";
export default {
  data() {
    return {
      personList: [],
      loading: false,
      sexMap: {
        "0": "男",
        "1": "女"
      },
      // modifyIndex: null,
      personIndex: null,
      modifyTemp: {},
      dialogFormVisible: false,
      formLabelWidth: "120px",
      searchList: {
        name: "",
        idCard: ""
      },
      personTypeMap: {
        //户籍状态
        "0": "正常",
        "1": "已迁出",
        "2": "迁入"
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入电话", trigger: "blur" }],
        birthday: [
          {
            type: "string",
            required: true,
            message: "请选择出生日期",
            trigger: "change"
          }
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        nation: [{ required: true, message: "请输入籍贯", trigger: "blur" }],
        idCard: [
          { required: true, message: "请输入身份证号", trigger: "blur" }
        ],
        address: [{ required: true, message: "请输入现住址", trigger: "blur" }]
      },
      total: 0,
      pageNum: 1,
      pageSize: 5
    };
  },
  methods: {
    getList(n, isSearch) {
      // 获取户籍列表
      this.loading = true;
      var cloneList = storage.getStorage("person");
      if (isSearch) {
        if (this.searchList.name != "") {
          cloneList = cloneList.filter(
            item => item.name == this.searchList.name
          );
        }
        if (this.searchList.idCard != "") {
          cloneList = cloneList.filter(
            item => item.idCard == this.searchList.idCard
          );
        }
      }
      var resList =
        cloneList.length - (n - 1) * this.pageSize >= this.pageSize
          ? cloneList.slice((n - 1) * this.pageSize, this.pageSize * n)
          : cloneList.slice((n - 1) * this.pageSize);
      setTimeout(() => {
        this.personList = resList;
        this.total = cloneList.length;
        this.pageNum = n;
        this.loading = false;
      }, 500);
    },
    search() {
      if (
        this.searchList.name == "" &&
        this.searchList.mobile == "" &&
        this.searchList.selectTime == null
      ) {
        return this.getList(1);
      }
      this.getList(1, true);
    },
    showDialog(temp) {
      this.dialogFormVisible = true;
      this.modifyTemp = JSON.parse(JSON.stringify(temp));
      var arr = storage.getStorage("person");
      var n;
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].userId == this.modifyTemp.userId) {
          n = i;
          break;
        }
      }
      this.personIndex = n;
    },
    modify() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          setTimeout(() => {
            var arr = storage.getStorage("person");
            arr[this.personIndex] = this.modifyTemp;
            storage.setStorage("person", arr);
            this.dialogFormVisible = false;
            this.getList(1);
            this.$message.success("修改成功");
          }, 500);
        } else {
          return false;
        }
      });
    },
    delItem(index) {
      // 删除户籍信息
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          setTimeout(() => {
            this.personList.splice(index, 1);
            storage.setStorage("house", this.personList);
            this.$message.success("删除成功");
          }, 500);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    detail(id) {
      this.$router.push(`./person/detail/${id}`);
    },
    moveOut(id) {
      this.$confirm("此操作将迁出该户籍信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        var personArr = storage.getStorage("person");
        for (var i = 0; i < personArr.length; i++) {
          if (personArr[i].userId == id) {
            personArr[i].moveOut = utils.formatDate(new Date().getTime());
            personArr[i].personType = "1";
            personArr[i].moveIn = "";
            personArr[i].moveInAddress = "";
            break;
          }
        }
        setTimeout(() => {
          storage.setStorage("person", personArr);
          this.getList(1);
          this.$message({
            type: "success",
            message: "迁出成功!"
          });
        }, 500);
      });
    },
    handleCurrentChange(e) {
      this.pageNum = e;
      if (this.searchList.name == "" && this.searchList.mobile == "") {
        return this.getList(e);
      } else {
        this.getList(e, true);
      }
    }
  },
  mounted() {
    this.getList(1);
  }
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
